package com.lyw.container

import androidx.activity.viewModels
import androidx.databinding.Observable.OnPropertyChangedCallback
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.lu.library.base.BaseActivity
import com.lu.library.ext.navigation
import com.lyw.container.databinding.ActivityContainerBinding
import com.lyw.home.HomeFragment
import com.lyw.mine.MineRoutePaths
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ContainerActivity : BaseActivity<ActivityContainerBinding>() {


    private lateinit var containerAdapter:FragmentStateAdapter

    var fragments = mutableListOf<Fragment>()

    val containerVm by viewModels<ContainerVm>()
    override fun initViews() {
        super.initViews()

//        binding.viewPager.adapter = ContainerAdapter(this)
        fragments.add(HomeFragment())
//        fragments.add(DeviceFragment())
//        (MineRoutePaths.MINE_HOME.navigation() as Fragment?)?.let {mineFragment->
//            fragments.add(mineFragment)
//        }

        containerAdapter = object:FragmentStateAdapter(this){
            override fun getItemCount(): Int {
                return fragments.size
            }
            override fun createFragment(position: Int): Fragment {

                return fragments[position]
            }

        }
        binding.viewPager.registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                containerVm.changeIndex(position)
            }
        })
        binding.viewPager.adapter = containerAdapter
        binding.viewPager.isUserInputEnabled = false
    }

    override fun initListener() {
        super.initListener()
        binding.tvHome.setOnClickListener {
            binding.viewPager.setCurrentItem(0)
        }
        binding.ivHome.setOnClickListener {
            binding.viewPager.setCurrentItem(0)
        }
        binding.tvDevice.setOnClickListener {
            binding.viewPager.setCurrentItem(1)
        }
        binding.tvMine.setOnClickListener {
            binding.viewPager.setCurrentItem(2)
        }
        binding.ivMine.setOnClickListener {
            binding.viewPager.setCurrentItem(2)
        }
    }
    override fun initData() {
        containerVm.index.observe(this){
            binding.tvHome.alpha = if(it==0){1f}else{0.5f}
            binding.tvDevice.alpha = if(it==1){1f}else{0.5f}
            binding.tvMine.alpha = if(it==2){1f}else{0.5f}
        }
    }

    override fun layoutResId() = R.layout.activity_container
}